package simulator;

import java.util.List;
import java.util.Vector;

import simulator.software.Process;
import simulator.hardware.Computer;
import simulator.software.ProcessState;
import simulator.software.ProcessorRunnable;

/**
 * <code>SimpleSchedulingScenario</code> provides a basic implemention of
 * <code>SchedulingScenario</code>.
 * 
 * @author Kyle Loomis
 * 
 */
public class SimpleSchedulingScenario implements SchedulingScenario
{
   protected String description;
   protected int currentStep;
   protected Computer computer;
   protected List<ProcessorRunnable> history;

   /**
    * Default constructor
    */
   public SimpleSchedulingScenario()
   {
      setDescription("Process Scheduling");
      history = new Vector<ProcessorRunnable>();
   }

   public int getCurrentStep()
   {
      return currentStep;
   }

   public String getDescription()
   {
      return description;
   }

   public void run()
   {
      // TODO: implement
      // run method not implemented until "completion" of a scenario
      // can be defined
   }

   public void seek(int stepNumber)
   {
      // TODO: implement
      // Seek not implemented
   }

   public void setDescription(String description)
   {
      this.description = description;
   }

   public void step()
   {
      // Capture metrics for process scheduling (cycles spent blocking, cycles
      // spent waiting)
      List<Process> processTable = computer.getKernel().getProcesses();

      for (Process p : processTable)
      {
         if (p.getState() == ProcessState.BLOCKED)
         {
            p.setCyclesBlocked(p.getCyclesBlocked() + 1);
         }
         else if (p.getState() == ProcessState.READY)
         {
            p.setCyclesWaiting(p.getCyclesWaiting() + 1);
         }
      }

      currentStep++;
      
      // Move computer state forward one state
      computer.step();
   }

   public Computer getComputer()
   {
      return computer;
   }

   public void setComputer(Computer computer)
   {
      this.computer = computer;
   }
}